package com.demo.config;

import org.apache.commons.configuration.AbstractConfiguration;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.netflix.config.ConfigurationManager;
import com.netflix.loadbalancer.RoundRobinRule;

/**
 * @author: demo
 * @date: 2020年7月3日
 * @Document : 测试路由配置类
 */
@SuppressWarnings("unused")
@Configuration
public class RoutesConfig {

	/**
	 * 模拟固定转发 ==>与配置文件类同
	 * 
	 * @param builder
	 * @return
	 */
//	@Bean
//	public RouteLocator myRoutes(RouteLocatorBuilder builder) {
//	    return builder.routes()
//	        .route(p -> p
//	            .path("/getRoute")
//	            .uri("http://192.168.63.104:999/"))
//	        .route(p -> p
//		            .path("/aaa/postRoute")
//                 .filters(f->f.stripPrefix(1))
//		            .uri("http://192.168.63.104:999/"))
//	        .build();
//	}

	/**
	 * 模拟负载均衡
	 * 
	 * @param builder
	 * @return
	 */
	@Bean
	public RouteLocator ribbonRoutes(RouteLocatorBuilder builder) {
		AbstractConfiguration config = ConfigurationManager.getConfigInstance();
		config.setProperty("default-ribbon-config.ribbon.listOfServers",
				"http://192.168.63.104:999,http://192.168.63.104:9993");
		config.setProperty("default-load-balance.ribbon.NFLoadBalancerRuleClassName", RoundRobinRule.class.getName());
		return builder.routes().route(p -> p.path("/ribbon/test")
				.uri("lb://default-load-balance"))
				.build();
	}
}
